var moment = require('moment');
var common=require('./common.js');
var userList = function(req, res, next){
	console.log(req.params.id);
	var id=req.params.id
	req.models.User.find({'id':id},function(err, people){
		return res.send(200,[people]);
	})
}


exports.userList = userList;

var bigfenlei = function(req, res, next){
	var data={
		username:'张慧敏',
		password:'123456',
		fontHome:"",
		fontIndex:"font-color",
		am_active:"",
		title: '-HOME',
		bigfenlei:[],
	}
	var tmp=[];
	req.models.bigfenlei.find(function(err, bigfenlei){
        //随机得到四条记录显示页面
        for(var i=0;i<bigfenlei.length;i++){
        	if(bigfenlei[i].content.length>70){
        		bigfenlei[i].content=bigfenlei[i].content.substring(0,70)+"...";
        	}
        }
        if(bigfenlei.length>4){
        	while(tmp.length<4){
        		var t=parseInt(Math.random()*bigfenlei.length);

        		if (tmp.indexOf(t)<0){
        			tmp.push(t);
        			data.bigfenlei.push(bigfenlei[t]);
        		}
        	}
        }
        else{
        	data.bigfenlei=bigfenlei;
        }
        res.render('home', { data:data });
    })
}
exports.bigfenlei = bigfenlei;

var  Article= function(req, res, next){
	var data={
		fontHome:"font-color",
		fontIndex:"font-home am-active",
		am_active:"am-active",
		author:'张慧敏',
		url:"127.0.0.1",
		wenzhang:"",
		biaoqian:"",
		biaoqianid:"",
	}
	var id=req.params.id;
	req.models.WenZhang.find({'id':id},function(err, wenzhang){
		data.wenzhang=wenzhang;
		req.models.biaoqian.find({'id':data.wenzhang[0].biaoqian_table},function(err, biaoqian){
				// data放下面  先处理数据库查询数据  然后将处理的数据赋值给将要渲染页面的数据
				data.biaoqianid=biaoqian[0].id;
				data.biaoqian=biaoqian[0].content.split(',');
				console.log(data.biaoqian);
				res.render('article',{ data: data});
			})
	})
}
exports.Article = Article;

var  Index= function(req, res, next){
	var data={
		fontHome:"font-color",
		fontIndex:"font-home am-active",
		am_active:"am-active",
		author:'张慧敏',
		url:"127.0.0.1",
		wenzhang:[],
		biaoqian:[],
		ids:[],
		next:0,
		after:0,
		id:0,
		title: '-INDEX',
		smallfenlei:[]
	}
	
	data["datearray"]=new Array();
	data["datearraycount"]=new Array(12);

	//data["titlearray"]=new Array();
	//data["titlearraycount"]=new Array(12);

	for(var i=0;i<data["datearraycount"].length;i++)
	{
		data["datearraycount"][i]=0;
	}
	/*
	for(var i=0;i<data["titlearraycount"].length;i++)
	{
		data["titlearraycount"][i]=0;
	}
	*/
	var id=+req.params.id;
	data.id=id;
	var where_wenzhang={};
	//如果id为0 说明显示的是所有文章的数据，不需要查询条件
	//如果不为0 说明是分类显示
	if(id==0){
		where_wenzhang={};
	}
	else{
		where_wenzhang={'bigfenlei':id};
	}
	var page=+req.params.page;
	req.models.WenZhang.count(where_wenzhang, function (err, count) {
		var allpage=Math.ceil(count/3);
		// 分页
		common.page(data,page,allpage);	
		//哪里不懂看这个页面 https://github.com/dresende/node-orm2
		req.models.WenZhang.find(where_wenzhang,[ "id", "Z" ]).limit(6).offset(6*(page-1)).run(function(err, wenzhang){
			wenzhangzifu(data,wenzhang);
			req.models.biaoqian.find(function(err, biaoqian){
					// data放下面  先处理数据库查询数据  然后将处理的数据赋值给将要渲染页面的数据
					data.biaoqian=biaoqian;
					req.models.WenZhang.find([ "id", "Z" ],function(err,wenzhangall){
						wenzhangcount(data,wenzhangall)
							req.models.smallfenlei.find(function(err,smallfenlei){
								for (var i = 0; i < smallfenlei.length; i++) {
									var tmp={};
									tmp["data"]=smallfenlei[i].title;
									tmp["sum"]=0;
									tmp["id"]=smallfenlei[i].id;
									for (var a = wenzhangall.length - 1; a > 0; a--) {
										if (wenzhangall[a].fenlei_id==smallfenlei[i].id) {
											tmp["sum"]=tmp["sum"]+1;
										}
									}
									data["smallfenlei"].push(tmp);
								}
								res.render('index',{ data: data});
							})
						
					})
					
				})
			
		})
	})
}
exports.index = Index;


function wenzhangzifu(data,wenzhang){
	for(var i=0;i<wenzhang.length;i++){
		if(wenzhang[i].content.length>70){
			wenzhang[i].content=wenzhang[i].content.substring(0,70)+"...";
		}
	}
	data.wenzhang=wenzhang;
}

function wenzhangcount(data,wenzhangall){

	for(var i=0;i<wenzhangall.length;i++){
		//哪里不懂看这个页面 http://momentjs.cn/
		//文章内容目录归类
		//这里你想错了  文章有日期  文章有标签 文章有分类，侧边栏就是显示这三样，每一篇文章都是独一无二的，不需要侧边栏显示了
		//wenzhang和bigfenlei是一对一的关系 wenzhang和smallfenlei也是一对一的关系 wenzhang和biapqian是一对多的关系，好比 一篇文章的biaoqian_table字段是1,2,3 那一篇文章对应的标签就是biaoqian表的content字段 
		//
		moment.locale("zh-cn"); 
		var datedb =moment(wenzhangall[i].pub_date).year()+","+(moment(wenzhangall[i].pub_date).month()+1);
		/*
		var titledb=wenzhangall[i].title;
		if(data["titlearray"].indexOf(titledb)==-1){
			data["titlearraycount"][wenzhangall[i].title]=1;
			data["titlearray"].push(titledb);
		}else{
			data["titlearraycount"][wenzhangall[i].title]=data["titlearraycount"][wenzhangall[i].title]+1;
		}
		*/
		//文章内容时间归类
		if(data["datearray"].indexOf(datedb)==-1){
			
			data["datearraycount"][moment(wenzhangall[i].pub_date).month()]=1;
			data["datearray"].push(datedb);
		}
		else{
			data["datearraycount"][moment(wenzhangall[i].pub_date).month()]=data["datearraycount"][moment(wenzhangall[i].pub_date).month()]+1;
		}
	}
	console.log(data["titlearray"]);
}
